package de.bazinga.rotw.effects;

import java.util.Enumeration;
import java.util.Hashtable;

import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Ring;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jmex.effects.particles.ParticleFactory;
import com.jmex.effects.particles.ParticleSystem;

public class ParticleEffects {
	public static ParticleEffects particleEffects = null;
	
	private Hashtable<String,Node> particleNodes = new Hashtable<String,Node>();
	private Hashtable<String,ParticleSystem> particleSys = new Hashtable<String,ParticleSystem>();
	
	public static ParticleEffects getInstance() {
		if(particleEffects == null) {
			particleEffects = new ParticleEffects();
		}
		return particleEffects;
	}
	
	
	public ParticleEffects() {       
        
		// Bounce
		ParticleSystem particleGeom = ParticleFactory.buildParticles("Hit", 100);
		particleGeom.setGeometry(new Ring(new Vector3f(0,0,0),
				new Vector3f(0,0.1f,0),
				0.1f,
				0.2f));
		particleGeom.setEmissionDirection(new Vector3f(0,0,0.01f));
		
		
		float pi = 3.14159265f;
        particleGeom.setMinimumAngle(0);
        particleGeom.setMaximumAngle((pi));
        particleGeom.setInitialVelocity(0.005f);
        particleGeom.setMinimumLifeTime(50.0f);
        particleGeom.setMaximumLifeTime(90.0f);
        particleGeom.setStartSize(0.05f);
        particleGeom.setEndSize(0.02f);
        particleGeom.setStartColor(ColorRGBA.red);
        particleGeom.setEndColor(ColorRGBA.red);
        particleGeom.getParticleController().setControlFlow(false);
        particleGeom.getParticleController().setRepeatType(
                Controller.RT_CLAMP);
        
        Node particleNode = new Node("Hit");
        particleNode.attachChild(particleGeom);
        
        Quaternion rot = new Quaternion();
        rot.fromAngleAxis(FastMath.DEG_TO_RAD * 90, Vector3f.UNIT_X);
        particleNode.setLocalRotation(rot);
        
        particleNode.updateRenderState();
        
        
        add("hit",particleNode,particleGeom);
	}
	
	
	
	public void add(String name,Node node,ParticleSystem partSys) {
		particleNodes.put(name, node);
		particleSys.put(name, partSys);
	}
	
	
	public Node getParticleNode(String name) {
		if(particleNodes.contains(name)) return null;
		return particleNodes.get(name);
	}
	
	public ParticleSystem getParticleSystem(String name) {
		if(particleSys.contains(name)) return null;
		return particleSys.get(name);
	}
	
	public void remove(String name) {
		particleNodes.remove(name);
		particleSys.remove(name);
	}
	
	
	public void addToNode(Node node) {
		Enumeration<Node> e = particleNodes.elements();
		while(e.hasMoreElements()) {
			node.attachChild(e.nextElement());
		}
	}
	
	public void addToNode(Node node,String name) {
		node.attachChild(particleNodes.get(name));
	}
}
